package xml;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import dominioTO.ClienteTO;


public class ClienteXmlDao {
	private static final String ARCHIVO = "http://localhost:8080/ServidorNegocio/xml/AltaClientes.xml";
	private static ClienteXmlDao instancia;
	private Document doc;
	
	public static ClienteXmlDao getInstancia(){
		if(instancia == null){
			instancia = new ClienteXmlDao();
			instancia.crearDocumento();
		}
		return instancia;
	}
	
	private void crearDocumento(){
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder;
		try {
			builder = factory.newDocumentBuilder();
			doc = builder.parse(ARCHIVO);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
		
	public List<ClienteTO> recuperarClientes(){
		List<ClienteTO> clientes = new ArrayList<ClienteTO>();
		NodeList nodos = doc.getElementsByTagName("Cliente");
		for(int i = 0; i < nodos.getLength(); i++){
			if(nodos.item(i).hasChildNodes()){
				clientes.add(mapCliente((Element)nodos.item(i)));
			}
		}
		return clientes;
	}
		
	private ClienteTO mapCliente(Element eleCli){
		ClienteTO cli = new ClienteTO();
		cli.setCuit(eleCli.getElementsByTagName("cuit").item(0).getTextContent());
		cli.setRazonSocial(	eleCli.getElementsByTagName("razonSocial").item(0).getTextContent());
		cli.setFechaAlta(Date.valueOf(eleCli.getElementsByTagName("fechaAlta").item(0).getTextContent()));
		cli.setDescuentos(Float.valueOf(eleCli.getElementsByTagName("descuentos").item(0).getTextContent()));
		cli.setRespInscripto(Boolean.parseBoolean(eleCli.getElementsByTagName("respInscripto").item(0).getTextContent()));
		cli.setCondPago(Integer.valueOf(eleCli.getElementsByTagName("condPago").item(0).getTextContent()));
		cli.setIngBrutos(Float.valueOf(eleCli.getElementsByTagName("ingBrutos").item(0).getTextContent()));
		
		cli.setEstado(Boolean.parseBoolean(eleCli.getElementsByTagName("estado").item(0).getTextContent()));
		return cli;
	}
	
	/*public void persistirClientes(Set<Cliente> clientes){
		Document doc = nuevoDocumento();
		Element root = doc.createElement("Clientes");
		doc.appendChild(root);
		for(Cliente cliente : clientes){
			Element usu = doc.createElement("Cliente");
			usu.setAttribute("Id", cliente.getId());
			root.appendChild(usu);
			Element usunom = doc.createElement("Usu-nombre");
			usunom.setTextContent(cliente.getNombre());
			usu.appendChild(usunom);
			Element usumail = doc.createElement("Usu-email");
			usumail.setTextContent(cliente.getMail());
			usu.appendChild(usumail);
		}
		aXML(doc);
	}
	
	private Document nuevoDocumento(){
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder;
		try {
			builder = factory.newDocumentBuilder();
			Document doc = builder.newDocument();
			return doc;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}*/
	
	/*private void aXML(Document doc){
		try {
			Transformer transformer = TransformerFactory.newInstance().newTransformer();
			Source src = new DOMSource(doc);
			Result dest = new StreamResult(new FileWriter(new File(ARCHIVO)));
			transformer.transform(src, dest);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/
}
